﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebMVP
{
    public class GreetingFormPresenter
    {
        private readonly IGreetingForm _greetingForm;
        private readonly IModel _model;

        public GreetingFormPresenter(IGreetingForm greetingForm, IModel model)
        {
            _greetingForm = greetingForm;
            _model = model;

            _greetingForm.EvtButtonPressed += new EventHandler<EventArgs>(_greetingForm_EvtButtonPressed);
        }

        // каждый раз при создании Presenter'а создается его Model
        public GreetingFormPresenter(IGreetingForm greetingForm)
            : this(greetingForm, new Model())
        {
        }

        /// <summary>
        /// Заполняет GreetingForm начальными значениями.
        /// </summary>
        public void Initialize()
        {
            _greetingForm.GreetingText = "The simplest Web Forms MVP example. Greeting will appear here.";
        }

        /// <summary>
        /// При нажатии кнопки забираем из модели необходимые данные.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _greetingForm_EvtButtonPressed(object sender, EventArgs e)
        {
            _greetingForm.GreetingText = _model.Greeting;
        }
    }
}